package com.myk.game.gobangchess.dates;

import com.myk.game.gobangchess.constants.PathCost;
import com.myk.game.gobangchess.utils.DateTimeUtil;
import com.myk.game.gobangchess.utils.FileUtils;
import com.myk.game.gobangchess.utils.StringUtils;

import java.io.File;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

public class NotificationData {
    /**
     * 获取所有当前活跃的公告id (公告文件名就是它的id，文件里存储了公告内容)
     */
    public static String getActiveNotificationIds() {
        FileUtils.createOrExistsDir(PathCost.DATA_NOTIFICATION_DIR);
        List<File> fileList = FileUtils.listFilesInDir(PathCost.DATA_NOTIFICATION_DIR, false);
        fileList.removeIf(File::isDirectory);
        if (fileList.size() > 0) {
            return fileList.stream()
                    .map(FileUtils::getFileNameNoExtension)
                    .collect(Collectors.joining(";"));
        }
        return "-1";
    }

    /**
     * 获取所有当前活跃的公告（包括内容和id）
     */
    public static String getActiveNotifications() {
        FileUtils.createOrExistsDir(PathCost.DATA_NOTIFICATION_DIR);
        List<File> fileList = FileUtils.listFilesInDir(PathCost.DATA_NOTIFICATION_DIR, false);
        fileList.removeIf(File::isDirectory);
        if (fileList.size() > 0) {
            return fileList.stream()
                    .map(file -> FileUtils.getFileNameNoExtension(file) + "," + FileUtils.readFile2String(file))
                    .collect(Collectors.joining(";"));
        }
        return "-1";
    }

    /**
     * 根据id获取活跃的公告内容
     */
    public static String getActiveNotificationById(String id) {
        FileUtils.createOrExistsDir(PathCost.DATA_NOTIFICATION_DIR);
        String ntfFile = PathCost.DATA_NOTIFICATION_DIR + id + ".txt";
        String text = FileUtils.readFile2String(ntfFile);
        return StringUtils.isSpace(text) ? "-1" : text;
    }

    /**
     * 创建新公告存储到公告目录中（公告文件名就是它的id，文件里存储了公告内容）
     */
    public static void createNotification(String id, String text) {
        FileUtils.createOrExistsDir(PathCost.DATA_NOTIFICATION_DIR);
        String ntfFile = PathCost.DATA_NOTIFICATION_DIR + id + ".txt";
        FileUtils.createFileByDeleteOldFile(ntfFile);
        FileUtils.writeFileFromString(ntfFile, text);
    }

    /**
     * 将该公告移动到旧公告文件夹里
     */
    public static void closeNotificationById(String id) {
        FileUtils.createOrExistsDir(PathCost.DATA_NOTIFICATION_DIR);
        FileUtils.createOrExistsDir(PathCost.DATA_NOTIFICATION_OLD_DIR);
        String ntfFile = PathCost.DATA_NOTIFICATION_DIR + id + ".txt";
        if (FileUtils.isFileExists(ntfFile)) {
            String destFile = PathCost.DATA_NOTIFICATION_OLD_DIR + id + ".txt";
            FileUtils.move(ntfFile, destFile);
        }
    }

    /**
     * 获取所有过时的公告id列表
     */
    public static String getOldNotificationIds() {
        FileUtils.createOrExistsDir(PathCost.DATA_NOTIFICATION_DIR);
        FileUtils.createOrExistsDir(PathCost.DATA_NOTIFICATION_OLD_DIR);
        List<File> fileList = FileUtils.listFilesInDir(PathCost.DATA_NOTIFICATION_OLD_DIR, false);
        fileList.removeIf(File::isDirectory);
        if (fileList.size() > 0) {
            return fileList.stream()
                    .map(FileUtils::getFileNameNoExtension)
                    .collect(Collectors.joining(";"));
        }
        return "-1";
    }

    /**
     * 根据id获取过时的公告内容
     */
    public static String getOldNotificationById(String id) {
        FileUtils.createOrExistsDir(PathCost.DATA_NOTIFICATION_DIR);
        FileUtils.createOrExistsDir(PathCost.DATA_NOTIFICATION_OLD_DIR);
        String ntfFile = PathCost.DATA_NOTIFICATION_OLD_DIR + id + ".txt";
        String text = FileUtils.readFile2String(ntfFile);
        return StringUtils.isSpace(text) ? "-1" : text;
    }

    public static void main(String[] args) {
        createNotification("20231129105852", "不是特别的开心");
        createNotification("20231129105920", "今天的太阳非常的圆");
//        System.out.println(getActiveNotificationIds());
//        System.out.println(getActiveNotificationById("20231129105852"));
//        closeNotificationById("20231129105852");

//        System.out.println(getActiveNotifications());
//        System.out.println(getOldNotificationIds());
        System.out.println(getOldNotificationById("20231129105920"));
        System.out.println(getOldNotificationById("20231129105852"));
    }


}
